﻿using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace NfsBLL
{
    /// <summary>
    /// sq_authorization
    /// </summary>
    public partial class Sq_authorizationBLL
    {
        private readonly NfsDAL.Sq_authorizationDAL dal = new NfsDAL.Sq_authorizationDAL();
        public Sq_authorizationBLL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(NfsModel.sq_authorization model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(NfsModel.sq_authorization model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(Maticsoft.Common.PageValidate.SafeLongFilter(idlist, 0));
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NfsModel.sq_authorization GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public NfsModel.sq_authorization GetModelByCache(int id)
        {

            string CacheKey = "sq_authorizationModel-" + id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (NfsModel.sq_authorization)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<NfsModel.sq_authorization> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<NfsModel.sq_authorization> DataTableToList(DataTable dt)
        {
            List<NfsModel.sq_authorization> modelList = new List<NfsModel.sq_authorization>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                NfsModel.sq_authorization model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  自定义方法
        #region  分页和模糊查询的方法
        public List<sq_authorization> GetSqAuBycondtion(int pageindex, int pagesize, string Name, string mobile, string level_id, string state)
        {
            DataTable dt = dal.GetSqAuBycondtion(pageindex, pagesize, Name, mobile, level_id, state);
            return ConverToExcs.ConvertToEx<sq_authorization>(dt);
        }
        public object GetSqAuBycondtionCount(string Name, string mobile, string level_id, string state)
        {
            return dal.GetSqAuBycondtionCount(Name, mobile, level_id, state);
        }
        #endregion
        #region 逻辑删除
        public bool delSqAuthorization(int id)
        {
            return dal.delSqAuthorization(id);
        }
        #endregion
        #region 同意或者拒绝更新
        public bool SqAuthorization(int id, int state)
        {
            return dal.SqAuthorization(id, state);
        }
        #endregion
        #region 同意和更新
        public bool updateShouQuan(int id, int state, out int agency_id)
        {

            bool flag = true;
            int agId = 0;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    flag = this.SqAuthorization(id, state);
                    sq_authorization model = this.GetModel(id);
                    if (model != null)
                    {
                        agId = (int)model.agency_id;

                        if (flag && state == 1)
                        {
                            AgencyBLL agbll = new AgencyBLL();

                            agency agmodel = agbll.GetModel((int)model.agency_id);
                            flag = agbll.UptPassOrNo_pc(agmodel.id, state, (int)agmodel.level_id, DateTime.Parse(model.start_time.ToString()), DateTime.Parse(model.end_time.ToString()), agmodel.level_name);

                        }
                    }
                    else
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        trans.Complete();
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }
            agency_id = agId;
            return flag;
        }
        #endregion


        #endregion  ExtensionMethod
    }
}

